package com.kelvin.paopao.module.market.market.locationpick;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kelvin.kelvinlib.utils.LogUtil;
import com.kelvin.paopao.R;
import com.kelvin.paopao.adapter.LocationPickAdapter;
import com.kelvin.paopao.api.responseBean.LocationPickModel;
import com.kelvin.paopao.injector.components.DaggerLocationPickComponent;
import com.kelvin.paopao.injector.modules.LocationPickModule;
import com.kelvin.paopao.module.base.BaseActivity;
import com.kelvin.paopao.module.market.market.EditRecieveLocation.EditRecieveLocationActivity;
import com.kelvin.paopao.widgets.CustomToolbar;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 收货地址选择的activity
 * Created by Administrator on 2017/5/16.
 */

public class LocationPickActivity extends BaseActivity<LocationPickPresenter>
        implements ILocationPickView {
    @BindView(R.id.custom_toolbar)
    CustomToolbar customToolbar;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    @Inject
    LocationPickAdapter adapter;

    private LinearLayoutManager manager;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_location_pick;
    }

    @Override
    protected void initInjector() {
        DaggerLocationPickComponent.builder().locationPickModule(new LocationPickModule(this))
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        customToolbar.showBackIcon(this);
        manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent();
                intent.putExtra("pick",(LocationPickModel)adapter.getData().get(position));
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.getData(false);
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @Override
    public void loadData(List<LocationPickModel> models) {
        adapter.setNewData(models);
    }

    @Override
    public void showEmpty() {
        View nodataView = LayoutInflater.from(this).inflate(R.layout.view_no_data,
                (ViewGroup) recyclerView.getParent(), false);
        adapter.setEmptyView(nodataView);
    }

    public static final int REQUEST_CODE_TO_LP = 1001;

    public static void launchForResult(Activity activity) {
        Intent intent = new Intent(activity, LocationPickActivity.class);
        activity.startActivityForResult(intent, REQUEST_CODE_TO_LP);
    }

    @OnClick(R.id.tv_add)
    public void onClick() {
        EditRecieveLocationActivity.launch(this);
    }
}
